<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>swap</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="prev" href="scoped_enum.html" title="scoped_enum">
<link rel="next" href="typeinfo.html" title="typeinfo">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="scoped_enum.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="typeinfo.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="core.swap"></a><a class="link" href="swap.html" title="swap">swap</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="swap.html#core.swap.header_boost_core_swap_hpp">Header &lt;boost/core/swap.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.rationale">Rationale</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.exception_safety">Exception Safety</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.requirements">Requirements</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.portability">Portability</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.credits">Credits</a></span></dt>
</dl></div>
<div class="simplesect">
<div class="titlepage"><div><div><h3 class="title">
<a name="idm46136446304896"></a>Authors</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Niels Dekker
        </li>
<li class="listitem">
          Joseph Gauterin
        </li>
<li class="listitem">
          Steven Watanabe
        </li>
<li class="listitem">
          Eric Niebler
        </li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.header_boost_core_swap_hpp"></a><a class="link" href="swap.html#core.swap.header_boost_core_swap_hpp" title="Header &lt;boost/core/swap.hpp&gt;">Header &lt;boost/core/swap.hpp&gt;</a>
</h3></div></div></div>
<p>
        <code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&amp;</span>
        <span class="identifier">right</span><span class="special">);</span></code>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.introduction"></a><a class="link" href="swap.html#core.swap.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
        The template function <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code>
        allows the values of two variables to be swapped, using argument dependent
        lookup to select a specialized swap function if available. If no specialized
        swap function is available, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
        is used.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.rationale"></a><a class="link" href="swap.html#core.swap.rationale" title="Rationale">Rationale</a>
</h3></div></div></div>
<p>
        The generic <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code> function requires that the elements
        to be swapped are assignable and copy constructible. It is usually implemented
        using one copy construction and two assignments - this is often both unnecessarily
        restrictive and unnecessarily slow. In addition, where the generic swap implementation
        provides only the basic guarantee, specialized swap functions are often able
        to provide the no-throw exception guarantee (and it is considered best practice
        to do so where possible <a href="#ftn.core.swap.rationale.f0" class="footnote" name="core.swap.rationale.f0"><sup class="footnote">[1]</sup></a>.
      </p>
<p>
        The alternative to using argument dependent lookup in this situation is to
        provide a template specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
        for every type that requires a specialized swap. Although this is legal C++,
        no Boost libraries use this method, whereas many Boost libraries provide
        specialized swap functions in their own namespaces.
      </p>
<p>
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> also supports swapping built-in arrays.
        Note that <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code> originally did not do so, but a request
        to add an overload of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
        for built-in arrays has been accepted by the C++ Standards Committee<a href="#ftn.core.swap.rationale.f1" class="footnote" name="core.swap.rationale.f1"><sup class="footnote">[2]</sup></a>.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.exception_safety"></a><a class="link" href="swap.html#core.swap.exception_safety" title="Exception Safety">Exception Safety</a>
</h3></div></div></div>
<p>
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> provides the same exception guarantee
        as the underlying swap function used, with one exception; for an array of
        type <code class="computeroutput"><span class="identifier">T</span><span class="special">[</span><span class="identifier">n</span><span class="special">]</span></code>, where
        <code class="computeroutput"><span class="identifier">n</span> <span class="special">&gt;</span>
        <span class="number">1</span></code> and the underlying swap function
        for <code class="computeroutput"><span class="identifier">T</span></code> provides the strong
        exception guarantee, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code>
        provides only the basic exception guarantee.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.requirements"></a><a class="link" href="swap.html#core.swap.requirements" title="Requirements">Requirements</a>
</h3></div></div></div>
<p>
        Either:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            T must be assignable
          </li>
<li class="listitem">
            T must be copy constructible
          </li>
</ul></div>
<p>
        Or:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            A function with the signature <code class="computeroutput"><span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;,</span><span class="identifier">T</span><span class="special">&amp;)</span></code> is available via argument dependent
            lookup
          </li></ul></div>
<p>
        Or:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            A template specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
            exists for T
          </li></ul></div>
<p>
        Or:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            T is a built-in array of swappable elements
          </li></ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.portability"></a><a class="link" href="swap.html#core.swap.portability" title="Portability">Portability</a>
</h3></div></div></div>
<p>
        Several older compilers do not support argument dependent lookup. On these
        compilers <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> will call <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>,
        ignoring any specialized swap functions that could be found as a result of
        argument dependent lookup.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.credits"></a><a class="link" href="swap.html#core.swap.credits" title="Credits">Credits</a>
</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <span class="bold"><strong>Niels Dekker</strong></span> - for implementing and
            documenting support for built-in arrays
          </li>
<li class="listitem">
            <span class="bold"><strong>Joseph Gauterin</strong></span> - for the initial idea,
            implementation, tests, and documentation
          </li>
<li class="listitem">
            <span class="bold"><strong>Steven Watanabe</strong></span> - for the idea to make
            <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> less specialized than <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>, thereby allowing the function
            to have the name 'swap' without introducing ambiguity
          </li>
</ul></div>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.core.swap.rationale.f0" class="footnote"><p><a href="#core.swap.rationale.f0" class="para"><sup class="para">[1] </sup></a>
          Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support
          for a non-throwing swap"
        </p></div>
<div id="ftn.core.swap.rationale.f1" class="footnote"><p><a href="#core.swap.rationale.f1" class="para"><sup class="para">[2] </sup></a>
          <a href="http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#809" target="_top">LWG
          Defect Report 809: std::swap should be overloaded for array types</a>
        </p></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p>
        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
        Software License, Version 1.0</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="scoped_enum.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="typeinfo.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
